package cn.edu.sgu.www.pms.controller;

import cn.edu.sgu.www.pms.consts.CustomConstants;
import cn.edu.sgu.www.pms.consts.CustomMessages;
import cn.edu.sgu.www.pms.consts.MimeTypes;
import cn.edu.sgu.www.pms.dto.RoleMenuDTO;
import cn.edu.sgu.www.pms.entity.Menu;
import cn.edu.sgu.www.pms.pager.impl.RoleMenuPager;
import cn.edu.sgu.www.pms.restful.JsonPage;
import cn.edu.sgu.www.pms.restful.JsonResult;
import cn.edu.sgu.www.pms.service.RoleMenuService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Api(tags = "角色菜单管理")
@ApiSupport(author = CustomConstants.AUTHOR)
@RestController
@RequestMapping(path = "/role_menu", produces = MimeTypes.APPLICATION_JSON_CHARSET_UTF_8)
public class RoleMenuController {

    private final RoleMenuService roleMenuService;

    @Autowired
    public RoleMenuController(RoleMenuService roleMenuService) {
        this.roleMenuService = roleMenuService;
    }

    @ApiOperation("保存角色的菜单")
    @RequestMapping(path = "/save", method = RequestMethod.POST)
    public JsonResult<Void> save(@Validated RoleMenuDTO roleMenuDTO) {
        roleMenuService.save(roleMenuDTO);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("取消角色的菜单")
    @RequestMapping(path = "/cancel", method = RequestMethod.POST)
    public JsonResult<Void> cancel(@Validated RoleMenuDTO roleMenuDTO) {
        roleMenuService.cancel(roleMenuDTO);

        return JsonResult.success(CustomMessages.OPERATE_SUCCESS);
    }

    @ApiOperation("分页查询角色的菜单")
    @RequestMapping(path = "/selectByRoleId", method = RequestMethod.GET)
    public JsonResult<JsonPage<Menu>> selectByRoleId(RoleMenuPager pager) {
        Page<Menu> page = roleMenuService.selectByRoleId(pager);

        return JsonResult.restPage(page);
    }

    @ApiOperation("分页查询角色未分配的菜单")
    @RequestMapping(path = "/selectUnselected", method = RequestMethod.GET)
    public JsonResult<JsonPage<Menu>> selectUnselected(RoleMenuPager pager) {
        Page<Menu> page = roleMenuService.selectUnselected(pager);

        return JsonResult.restPage(page);
    }

}